package com.imooc.authorization.controller;

import com.imooc.authorization.common.AjaxResult;
import com.imooc.authorization.entity.User;
import com.imooc.authorization.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collection;

/**
 * Created by Administrator on 2016/8/23.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired private UserService userService;

    @RequestMapping(value="/index")
    public String userList(){

        return "security/user/user_list";
    }

    @RequestMapping(value = "/addEditUser",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult addEditUser(User user){
        if(null==user.getId()){
            userService.addUser(user);
        }else {
            userService.updateUser(user);
        }
        return AjaxResult.success();
    }

    @RequestMapping(value = "/deleteUser",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult deleteUser(Long id){
        userService.deleteUserBy(id);

        return AjaxResult.success();
    }

    @RequestMapping(value = "/getUsers",method = RequestMethod.POST)
    @ResponseBody
    public Collection<User> getUsers(Integer page,Integer rows){

        return userService.getUsers(page,rows);
    }
}
